//
//  OilCardQueryViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/11.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "OilCardQueryViewController.h"
#import "OilCardQueryListTableViewController.h"
#import "WXFormat.h"
#import "OilCardPayViewController.h"

@interface OilCardQueryViewController () <UITextFieldDelegate>

@end

@implementation OilCardQueryViewController

- (void)viewWillAppear:(BOOL)animated{
     UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _oilCardNum.delegate = self;
    _oilCardNum.tag = 300;
    if (_Type == 2) {
        self.title = @"油卡";
     }else {
        self.title = @"绑定油卡";
     }
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (IBAction)queryClick:(id)sender {
    [self.view endEditing:YES];
    if (_oilCardName.text.length <= 0) {
        [self.view makeToast:@"请输入姓名" duration:1.5 position:TOP_POINT];
        return;
    }
    if (![WXFormat validateNickname:_oilCardName.text]) {
        [self.view makeToast:@"请输入正确的姓名" duration:1.5 position:TOP_POINT];
        return;
    }
    if (_oilCardNum.text.length <= 0) {
        [self.view makeToast:@"请输入卡号" duration:1.5 position:TOP_POINT];
        return;
    }
    if (_Type == 2) {
        if (_target) {
            NSDictionary *dic = @{@"CARD_NUMBER" : _oilCardNum.text , @"CARDHOLDER":_oilCardName.text};
            SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:dic]);
 
            [self backTo];
        }
        return;
    }
  
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    self.carAround.oilCard = _oilCardNum.text;
    self.carAround.name = _oilCardName.text;
    
    [self.carAround bindOilCard:^(id result) {
        NSLog(@"%@",result);
        [self.view endEditing:YES];
        [self.navigationController.view makeToast:@"绑定成功" duration:1 position:TOP_POINT];
        [self performSelector:@selector(backTo) withObject:nil afterDelay:1.3];
    } anfFail:^(id result) {
        
    }];
    
}
- (void)backTo {
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    if (textField.tag == 300) {
        //只能输入数字
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:Point]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
        BOOL canChange = [string isEqualToString:filtered];
        return canChange;
    }else{
        if (toBeString.length < 20) {
            return YES;
        }else{
            return NO;
        }
    }
    return YES;
}

//- (void)pushToOilCardList {
//    OilCardQueryListTableViewController *oil = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"oilCardList"];
//    oil.tableDataSouce = _tableDataSource;
//    [self.navigationController pushViewController:oil animated:YES];
//    
//}

@end
